package com.xiaoshuidi.cloud.module.iot.controller.admin.callback;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.module.iot.enums.KunShanMsgTypeEnums;
import com.xiaoshuidi.cloud.module.iot.service.KunShanCallbackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("callback")
public class KunShanDeviceController {

    private Logger LOGGER = LoggerFactory.getLogger(KunShanDeviceController.class);

    @Autowired
    private KunShanCallbackService kunShanCallbackService;

    @PostMapping("kunshan/event")
    public void event(@RequestBody JSONObject object) throws Exception {
        String msgType = object.getString("msgType");
        KunShanMsgTypeEnums instanceByCode = KunShanMsgTypeEnums.getInstanceByCode(msgType);
        if (null == instanceByCode) {
            LOGGER.error("琨山门锁回调的消息类型 = {} 未定义", msgType);
            return;
        } else {
            LOGGER.info("琨山回调请求，请求内容：{}", object);
            LOGGER.info("琨山门锁回调消息类型 = {}", instanceByCode.getDesc());
        }
        switch (msgType) {
            case "49":
            case "70":
            case "71":
                kunShanCallbackService.statusCallback(object);
                break;
            //在线密码
            case "53":
            case "55":
            case "54":
                kunShanCallbackService.operate(object);
                break;
            case "67":
                kunShanCallbackService.operate(object);
                break;
            //指纹密码
            case "59":
            case "57":
            case "58":
            case "66":
            case "61":
                kunShanCallbackService.fingerOperate(object);
                break;
            case "50":
                kunShanCallbackService.deviceMessage(object);
                break;
            default:
                LOGGER.info("其余类型{}-{} 的回调指令被忽略", msgType, instanceByCode.getDesc());
        }
    }
}


